/**
 * Copyright (c) 2011 - 2012 National Library of Medicine
 *
 * Contact: tomasz.adamusiak@nih.gov
 * 
 * This file is part of LOwlINC
 * 
 * LOwlINC is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * LOwlINC is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along
 * with LOwlINC. If not, see <http://www.gnu.org/licenses/>.
 */
package gov.nih.nlm.lowlinc.bean;

import gov.nih.nlm.lowlinc.accessionwrapper.LOINCPartID;
import gov.nih.nlm.lowlinc.owlapi.LowlincException;

import java.util.List;

import org.apache.log4j.Logger;

/**
 * @author Tomasz Adamusiak
 * 
 */
public class LOINCPart {
	private static final Logger log = Logger.getLogger(LOINCPart.class);

	private String L_PART_ID;
	private String PART_TYPE;
	private String NAME_CASE_INSENSITIVE;
	private String DISPLAY_NAME;
	private String SHORT_NAME_MIXED_CASE;

	// these are for multiaxial decomposition
	public Boolean isProcessed() {
		if (partComposition == null) {
			return false;
		}
		return true;
	}

	private List<LOINCPart> partComposition = null;

	public List<LOINCPart> getPartComposition() {
		return partComposition;
	}

	public void setPartComposition(List<LOINCPart> partComposition) {
		this.partComposition = partComposition;
	}

	@Override
	public String toString() {
		return "LOINC Part [" + L_PART_ID + " " + getPART_TYPE() + " "
				+ getLabel() + "]";
	}

	public String getL_PART_ID() {
		return L_PART_ID;
	}

	public LOINCPartID getPartID() throws LowlincException {
		return new LOINCPartID(getL_PART_ID());
	}

	public void setL_PART_ID(String l_PART_ID) {
		L_PART_ID = l_PART_ID;
	}

	public String getPART_TYPE() {
		return PART_TYPE;
	}

	public void setPART_TYPE(String pART_TYPE) {
		PART_TYPE = pART_TYPE;
	}

	public String getNAME_CASE_INSENSITIVE() {
		return NAME_CASE_INSENSITIVE;
	}

	public void setNAME_CASE_INSENSITIVE(String nAME_CASE_INSENSITIVE) {
		NAME_CASE_INSENSITIVE = nAME_CASE_INSENSITIVE;
	}

	public String getDISPLAY_NAME() {
		return DISPLAY_NAME;
	}

	public String getLabel() {
		if (!getNAME_CASE_INSENSITIVE().equals("")) {
			return getNAME_CASE_INSENSITIVE();
		} else {
			log.warn("NAME_CASE_INSENSITIVE empty in " + getL_PART_ID()
					+ " supplying DISPLAY_NAME instead");
			return getDISPLAY_NAME();
		}

	}

	public void setDISPLAY_NAME(String dISPLAY_NAME) {
		DISPLAY_NAME = dISPLAY_NAME;
	}

	public String getSHORT_NAME_MIXED_CASE() {
		return SHORT_NAME_MIXED_CASE;
	}

	public void setSHORT_NAME_MIXED_CASE(String sHORT_NAME_MIXED_CASE) {
		SHORT_NAME_MIXED_CASE = sHORT_NAME_MIXED_CASE;
	}

	public static String getOWLRelationName(String type)
			throws LowlincException {
		switch (type) {
		case "ADJUSTMENT":
			return "has_adjustment";
		case "CHALLENGE":
			return "has_challenge";
		case "CLASS":
			return "has_class";
		case "COMPONENT":
			return "has_component";
		case "DIVISORS":
			return "has_divisor";
		case "FRAGMENTS FOR SYNONYMS":
			return "has_fragments_for_synonyms";
		case "METHOD":
			return "has_method";
		case "PROPERTY":
			return "has_property";
		case "SCALE":
			return "has_scale";
		case "SUFFIX":
			return "has_suffix";
		case "SUPER SYSTEM":
			return "has_supersystem";
		case "SYSTEM":
			return "has_system";
		case "TIME":
			return "has_time_aspect";
		case "TIME MODIFIER":
			return "has_time_modifier";
		case "QUOTIENTS":
			return "has_quotients";
		case "MULTIPART":
			return "has_multipart";
		case "MULTI-AXIAL":
			return "has_multiaxial";
		default:
			throw new LowlincException("Uknown relation " + type);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ (L_PART_ID == null ? 0 : L_PART_ID.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		LOINCPart other = (LOINCPart) obj;
		if (L_PART_ID == null) {
			if (other.L_PART_ID != null) {
				return false;
			}
		} else if (!L_PART_ID.equals(other.L_PART_ID)) {
			return false;
		}
		return true;
	}

}
